package org.sgroup.imenu.controladapter;

import java.util.ArrayList;
import org.sgroup.imenu.bo.Item;
import org.sgroup.imenu.util.LoadBitmap;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class OrderListAdapter extends ArrayAdapter<Item> {

	Context context;
	int resourceid;
	ArrayList<Item> objects;

	public OrderListAdapter(Context context, int resource,
			ArrayList<Item> objects) {
		super(context, resource, objects);
		this.context = context;
		this.resourceid = resource;
		this.objects = objects;
		// TODO Auto-generated constructor stub
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		if (convertView == null) {
			LayoutInflater flater = LayoutInflater.from(context);
			convertView = flater.inflate(resourceid, null);
		}
		ImageView v1 = (ImageView) convertView
				.findViewById(org.sgroup.imenu.R.id.order_item_image);
		TextView t1 = (TextView) convertView
				.findViewById(org.sgroup.imenu.R.id.order_item_name);
		Button b1 = (Button) convertView
				.findViewById(org.sgroup.imenu.R.id.order_item_quantity);
		
		Item item = objects.get(position);
		t1.setText(item.Name);

		convertView.setId(item.ID);
		String _quantity = item.Quantity + "";
		if (item.Quantity < 10) {
			_quantity = "0" + item.Quantity;
		}
		b1.setText(_quantity);
		
		if (!item.ImageFile.isEmpty()) {
			try {								
				Bitmap bm= LoadBitmap.GetBitmap(this.context,item.ImageFile);
				v1.setImageBitmap(bm);
			} catch (Exception e) {
				Log.i("getView", e.getMessage());
			}

		}
		return convertView;
	}
}
